using System;
using System.Collections.Generic;
using System.Text;
using DAL; 
using DTO; 
using System.Data;
using System.Data.OleDb;

namespace BLL
{
    public class SINHVIENDAL
    {
        public List<SINHVIEN> GetAll()
        {
            SINHVIEN sinhvien;
            List<SINHVIEN> list = new List<SINHVIEN>();
            DataProvider dataprovider = new DataProvider();
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from SINHVIEN", null);
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                sinhvien = new SINHVIEN();
                sinhvien.Makhoa = Convert.ToByte(datatable.Rows[i]["MaSV"]);
                sinhvien.Tenkhoa = datatable.Rows[i]["TenSV"].ToString();
                list.Add(sinhvien);
            }
            return list;
        }

        public SINHVIEN GetSINHVIEN(byte makhoa)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[1];
            parameters[0] = new OleDbParameter("@masv", OleDbType.TinyInt);
            parameters[0].Value = makhoa;
            DataTable datatable = new DataTable();
            datatable = dataprovider.ExecuteSelectQuery("Select * from SINHVIEN where MaSV = @masv", parameters);
            SINHVIEN sinhvien = new SINHVIEN();
            if (datatable.Rows.Count != 0)
            {
                sinhvien.MaSV = Convert.ToByte(datatable.Rows[0]["MaSV"]);
                sinhvien.TenSV = datatable.Rows[0]["TenSV"].ToString();
                return sinhvien;
            }
            else
                return null;
        }

        public bool UpdateSINHVIEN(byte masv, string tensv)
        {
            DataProvider dataprovider = new DataProvider();
            OleDbParameter[] parameters = new OleDbParameter[3];
            parameters[0] = new OleDbParameter("@masv", OleDbType.TinyInt);
            parameters[0].Value = masv;
            parameters[1] = new OleDbParameter("@tensv", OleDbType.Char);
            parameters[1].Value = tensv;
            return dataprovider.ExecuteNonSelectQuery("update SINHVIEN set MaSV=@masv where Tenkhoa=@tenkhoa ", parameters);
        }
    }
}
